<script setup lang="ts">
import { ref, reactive, watch, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { dataAPI } from '@/http/leancloud';

interface VehicleForm {
  id?: string;
  vehicleNumber: string;
  vehicleType: string;
  driver: string;
  route: string;
  length: string;
  capacity: string;
  loadVolume: string;
  loadWeight: string;
  status: number;
  manufactureDate: string;
}

const props = defineProps<{
  visible: boolean;
  vehicleData?: VehicleForm | null;
  isEdit?: boolean;
}>();

const emit = defineEmits<{
  'update:visible': [value: boolean];
  'confirm': [data: VehicleForm];
}>();

// 表单数据
const formData = reactive<VehicleForm>({
  vehicleNumber: '',
  vehicleType: 'small_truck',
  driver: '',
  route: '',
  length: '',
  capacity: '',
  loadVolume: '',
  loadWeight: '',
  status: 1,
  manufactureDate: ''
});

// 表单引用
const formRef = ref();

// 车型选项
const vehicleTypeOptions = [
  { value: 'small_truck', label: '小型货车' },
  { value: 'medium_truck', label: '中型货车' },
  { value: 'large_truck', label: '大型货车' },
  { value: 'van', label: '面包车' }
];

// 状态选项
const statusOptions = [
  { value: 1, label: '正常' },
  { value: 2, label: '维修中' },
  { value: 0, label: '停用' }
];

// 表单验证规则
const rules = {
  vehicleNumber: [
    { required: true, message: '请输入车牌号', trigger: 'blur' },
  ],
  vehicleType: [
    { required: true, message: '请选择车型', trigger: 'change' }
  ],
  driver: [
    { required: true, message: '请输入司机姓名', trigger: 'blur' }
  ],
  route: [
    { required: true, message: '请输入绑定路线', trigger: 'blur' }
  ],
  length: [
    { required: true, message: '请输入车长', trigger: 'blur' }
  ],
  capacity: [
    { required: true, message: '请输入核定载客', trigger: 'blur' }
  ],
  loadVolume: [
    { required: true, message: '请输入核载体积', trigger: 'blur' }
  ],
  loadWeight: [
    { required: true, message: '请输入核载重量', trigger: 'blur' }
  ]
};

// 监听弹窗显示状态
watch(() => props.visible, (newVal) => {
  if (newVal) {
    resetForm();
    if (props.isEdit && props.vehicleData) {
      Object.assign(formData, props.vehicleData);
    }
  }
});

// 重置表单
const resetForm = () => {
  Object.assign(formData, {
    vehicleNumber: '',
    vehicleType: 'small_truck',
    driver: '',
    route: '',
    length: '',
    capacity: '',
    loadVolume: '',
    loadWeight: '',
    status: 1,
    manufactureDate: ''
  });
  formRef.value?.clearValidate();
};

// 关闭弹窗
const handleClose = () => {
  emit('update:visible', false);
};

// 确认提交
const handleConfirm = async () => {
  try {
    await formRef.value?.validate();
    emit('confirm', { ...formData });
    handleClose();
    ElMessage.success(props.isEdit ? '编辑成功' : '新增成功');
  } catch (error) {
    console.error('表单验证失败:', error);
  }
};
</script>

<template>
  <el-dialog
    :model-value="visible"
    :title="isEdit ? '编辑车辆' : '新增车辆'"
    width="600px"
    @update:model-value="handleClose"
    @close="handleClose"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="100px"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="车牌号" prop="vehicleNumber">
            <el-input v-model="formData.vehicleNumber" placeholder="请输入车牌号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车型" prop="vehicleType">
            <el-select v-model="formData.vehicleType" placeholder="请选择车型" style="width: 100%">
              <el-option
                v-for="item in vehicleTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="司机" prop="driver">
            <el-input v-model="formData.driver" placeholder="请输入司机姓名" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="绑定路线" prop="route">
            <el-input v-model="formData.route" placeholder="请输入绑定路线" />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="车长" prop="length">
            <el-input v-model="formData.length" placeholder="请输入车长" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出厂日期" prop="manufactureDate">
            <el-date-picker v-model="formData.manufactureDate" type="date" placeholder="请选择出厂日期" style="width: 100%" />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="核定载客" prop="capacity">
            <el-input v-model="formData.capacity" placeholder="请输入核定载客" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="核载体积" prop="loadVolume">
            <el-input v-model="formData.loadVolume" placeholder="请输入核载体积" />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="核载重量" prop="loadWeight">
            <el-input v-model="formData.loadWeight" placeholder="请输入核载重量" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-select v-model="formData.status" placeholder="请选择状态" style="width: 100%">
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">
          {{ isEdit ? '保存' : '确定' }}
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped>
.dialog-footer {
  text-align: right;
}
</style>